/* Copyright 2017, 2018, Gabriel Czernikier
 *
 * This file is part of Notehaus.
 * Notehaus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Notehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Notehaus.  If not, see <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "composer.h"
#include "lgwseq.h"
#include "types.h"

LIST_HEAD(str_id_head);
unsigned int str_id_list_size = 0;

struct str_id_st *add_str_id(lgwseq_t *for_str_dt) {
  struct str_dt_st *str_dt = malloc(sizeof(struct str_dt_st));
  str_dt->unpacked_data=lgwseq;
  struct str_id_st *str_id = malloc(sizeof(struct str_id_st));
  str_id->str_dt = str_dt;
  list_add(&str_id->str_id_list, &str_id_head);
  str_id_list_size++;
  return str_id;
}

int ostr_id_st_compar(struct ostr_id_st *ostr_id_1, struct ostr_id_st *ostr_id_2) {
  int cmp = lgwseq_t_compar(ostr_id_1->str_id->str_dt->unpacked_data, ostr_id_2->str_id->str_dt->unpacked_data);
  if(cmp==0)
    ostr_id_1->bool_duplicate=ostr_id_2->bool_duplicate=1;
  return cmp;
}

void build_ostr_id_and_pack_str_dt() {
  /* Walkthrough over str_dt_list is expensive.
   * Making both sorting and packing in one pass tries to come over the penalty.
   * Not that both transformations are really related one to the other, however.
   */
  ostr_id_ary = malloc((str_id_list_size+1/*reserves one more position*/)*sizeof(struct ostr_id_st));
  memset(ostr_id_ary, 0, (str_id_list_size+1)*sizeof(struct ostr_id_st));
  // Prepare an artificial element that when used to compute the relative_offset, the result is correct.
  ostr_id_ary->relative_offset =0;
  // Hide the temporarily intended array position (care of that upon memory deallocation, if ever performed).
  ostr_id_ary++;
  struct list_head *tmp;
  unsigned int i=0;
  list_for_each(tmp, &str_id_head) {
    (ostr_id_ary+i)->str_id = (struct str_id_st *)list_entry(tmp, struct str_id_st, str_id_list);
    i++;
  }
  qsort(ostr_id_ary, str_id_list_size, sizeof(struct ostr_id_st), (int (*)(const void *, const void *))ostr_id_st_compar);
  i=1;
  unsigned int major_i = 0, major_idx = 0;
  size_t packed_size=0;
  while(major_i!=str_id_list_size) {
    (ostr_id_ary+major_i)->relative_offset = (ostr_id_ary+major_i-1)->relative_offset+packed_size;
    packed_size = pack_string_data((ostr_id_ary+major_i)->str_id->str_dt->unpacked_data, &(ostr_id_ary+major_i)->str_id->str_dt->packed_data);
    BUFFER_WRITE(buffer[NH_STR_DT_IDX],buf_len[NH_STR_DT_IDX],(ostr_id_ary+major_i)->str_id->str_dt->packed_data,buf_offset[NH_STR_DT_IDX],packed_size)
    (ostr_id_ary+major_i)->str_id->idx = major_idx;
    while(i<str_id_list_size && (ostr_id_ary+i)->bool_duplicate && lgwseq_t_compar((ostr_id_ary+major_i)->str_id->str_dt->unpacked_data,(ostr_id_ary+i)->str_id->str_dt->unpacked_data)==0) {
      (ostr_id_ary+i)->duplicate_major = (ostr_id_ary+major_i);
      (ostr_id_ary+i)->relative_offset = (ostr_id_ary+major_i)->relative_offset;
      (ostr_id_ary+i)->str_id->idx = (ostr_id_ary+major_i)->str_id->idx;
      i++;
    }
    (ostr_id_ary+major_i)->bool_duplicate=0;   // Holds with the caluse: bool_duplicate can be set after the _compar function that updates this fields was called for the very last time on this position.
    if(i!=str_id_list_size)
      (ostr_id_ary+major_i)->next_major = ostr_id_ary+i;
    major_i=i++;
    major_idx++;
  }
  bounds_move(NH_STR_ID_IDX, sizeof(uint32_t)/*Storage Designator: (struct ostr_id_st).relative_offset+up_bounds_array[i]*/*major_idx);
  bounds_move(NH_STR_DT_IDX, (ostr_id_ary+major_i-1)->relative_offset + packed_size);
  majors_size_ary[NH_STR_ID_IDX] = major_idx;
}

void pack_ostr_id_ary() {
  for(struct ostr_id_st *ostr_id_major = ostr_id_ary;ostr_id_major!=NULL; ostr_id_major=ostr_id_major->next_major){
    uint32_t string_data_off = htole32(ostr_id_major->relative_offset+up_bounds_ary[NH_STR_DT_IDX-1]);
    BUFFER_WRITE(buffer[NH_STR_ID_IDX],buf_len[NH_STR_ID_IDX],&string_data_off,buf_offset[NH_STR_ID_IDX],sizeof(string_data_off));
  }
}


